var app = angular.module('BaseControl');

app.directive("textbox", [function() {
    return {
        restrict: "E",	// 指令是一个元素 (并非属性)，备选项有"A"、"E" 和 "C"， "M" ,分别代表attribute、element、class和comment（默认值为"A"）
        scope: {
            value: '=?',
            change: '&',
            blur: '&'
        },
        template: 		// 替换HTML (使用scope中的变量)
        '<input type="text" class="form-control BaseControl" oncontextmenu="return false;" placeholder="{{Placeholder}}" ' +
        'ng-show="Visible" ng-disabled="!Enabled" ' +
        'ng-model="value" ng-click="click()" ng-keydown="keyDown()" ng-focus="focus()" ng-blur="textBlur()"/>',
        replace: true,			// 使用模板替换原始标记
        transclude: true,		// 不复制原始HTML内容
        controller: ['$scope', function($scope){
            $scope.click = function() {
                //$scope.$digest();
            };

            $scope.textChange = function() {
                $scope.$digest();
                $scope.change();
            };

            $scope.focus = function(){
                event.srcElement.select();
            };

            $scope.textBlur = function(){
                // if(!$scope.IsEmpty){
                //     if($scope.value != undefined && $scope.value.trim() != ""){
                //         $scope.NotNullElement.style.border = "";
                //     }
                //     else{
                //         $scope.NotNullElement.style.border = "solid #F89885 1px";
                //     }
                // }
                $scope.blur();
            };

            $scope.keyDown = function(){
              if(event.keyCode == 13){
                  event.keyCode = 9;
              }
            };
        }],
        link: function (scope, element, attrs) {
            //scope.NotNullElement = element[0];
            scope.Enabled = true;
            scope.Visible = true;
            scope.IsEmpty = true;
            scope.Placeholder = '';

            if (attrs["isempty"] != undefined && attrs["isempty"].toLocaleLowerCase() == "false") {
                scope.IsEmpty = false;
            }
            if (attrs["placeholder"] != undefined && attrs["placeholder"].trim() != "") {
                scope.Placeholder = attrs["placeholder"].trim();
            }

            element[0].addEventListener('change', function(){scope.textChange()}, false);

            var watch = scope.$watch(
                function(){
                    return attrs["enabled"] || attrs["visible"];
                },
                function(){
                    if (attrs["enabled"] != undefined && attrs["enabled"].trim().toLocaleLowerCase() == "false") {
                        scope.Enabled = false;
                    }
                    else{
                        scope.Enabled = true;
                    }
                    if (attrs["visible"] != undefined && attrs["visible"].trim().toLocaleLowerCase() == "false") {
                        scope.Visible = false;
                    }
                    else{
                        scope.Visible = true;
                    }
                }
            );
            //watch() 注销监视
        }
    }
}]);
